﻿<%--<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRM.Models.TrainingsCourses>" %>--%>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CRM.Database.Model.TrainingsCourses>" %>
<%@ Import Namespace="CRM.Models" %>
<%@ Import Namespace="System.Drawing" %>

<%
    var placesCol = new CRM.Database.Model.TrainingsPlacesCollection(); 
%>

<script type="text/javascript">
//<![CDATA[
    var textSeparator = ";";
    function OnListBoxSelectionChanged1(listBox, args) {
        if (args.index == 0)
            args.isSelected ? listBox.SelectAll() : listBox.UnselectAll();
        UpdateSelectAllItemState1();
        UpdateText1();
    }
    function UpdateSelectAllItemState1() {
        IsAllSelected1() ? checkListBox1.SelectIndices([0]) : checkListBox1.UnselectIndices([0]);
    }
    function IsAllSelected1() {
        for (var i = 1; i < checkListBox1.GetItemCount(); i++)
            if (!checkListBox1.GetItem(i).selected)
                return false;
        return true;
    }
    function UpdateText1() {
        var selectedItems = checkListBox1.GetSelectedItems();
        Places.SetText(GetSelectedItemsText1(selectedItems));
    }
    function SynchronizeListBoxValues1(dropDown, args) {
        checkListBox1.UnselectAll();
        var texts = dropDown.GetText().split(textSeparator);
        var values = GetValuesByTexts1(texts);
        checkListBox1.SelectValues(values);
        UpdateSelectAllItemState1();
        UpdateText1(); // for remove non-existing texts
    }
    function GetSelectedItemsText1(items) {
        var texts = [];
        for (var i = 0; i < items.length; i++)
            if (items[i].index != 0)
                texts.push(items[i].text);
        return texts.join(textSeparator);
    }
    function GetValuesByTexts1(texts) {
        var actualValues = [];
        var item;
        for (var i = 0; i < texts.length; i++) {
            item = checkListBox1.FindItemByText(texts[i]);
            if (item != null)
                actualValues.push(item.value);
        }
        return actualValues;
    }
// ]]>
</script>

<%:
    Html.DevExpress().Label(
              settings =>
              {
                  settings.ControlStyle.CssClass = "lable";
                  settings.Text = "Список площадок";
                  settings.AssociatedControlName = "Places";
                  settings.Width = 300;

              }).GetHtml() %>

    <%: Html.DevExpress().DropDownEdit(
    settings =>
    {
        settings.Name = "Places";
        settings.Text = Model.Places.ToString();
        settings.Width = 300;
        settings.Properties.DropDownWindowStyle.BackColor = Color.FromArgb(0xEDEDED);
        settings.SetDropDownWindowTemplateContent(
            c =>
            {
                Html.DevExpress().ListBox(
                    listBoxSettings =>
                    {
                        listBoxSettings.Name = "checkListBox1";
                        listBoxSettings.SkinID = "CheckComboBoxListBox";
                        listBoxSettings.Properties.SelectionMode = ListEditSelectionMode.CheckColumn;
                        listBoxSettings.Properties.ClientSideEvents.SelectedIndexChanged = "OnListBoxSelectionChanged1";
                        listBoxSettings.ControlStyle.Border.BorderWidth = 0;
                        listBoxSettings.ControlStyle.BorderBottom.BorderWidth = 1;
                        listBoxSettings.ControlStyle.BorderBottom.BorderColor = Color.FromArgb(0xDCDCDC);
                        listBoxSettings.Width = Unit.Percentage(100);
                        listBoxSettings.Properties.DataSourceID = "IdTrainingsTrainer";
                        listBoxSettings.Properties.TextField = "NameRu";
                    }).BindList(placesCol.DropDownList).Bind(Model.Places).Render();
                    //}).BindList(DB.GetTrainingsPlacesForDropDownList()).Bind(Model.Places).Render();
                ViewContext.Writer.Write("<table style=\"width:100%\"><tr><td align=\"right\">");
                Html.DevExpress().Button(
                  buttonSettings =>
                  {
                      buttonSettings.Name = "buttonClose1";
                      buttonSettings.Text = "Выбрать";
                      buttonSettings.Style.Add("float", "right");
                      buttonSettings.ClientSideEvents.Click = "function(s, e){ Places.HideDropDown(); }";
                  }
                  ).Render();
                ViewContext.Writer.Write("</td></tr></table>");
            });
        settings.Properties.EnableAnimation = false;
        settings.Properties.ClientSideEvents.TextChanged = "SynchronizeListBoxValues1";
        settings.Properties.ClientSideEvents.DropDown = "SynchronizeListBoxValues1";
        
    }).Bind(Model.Places).GetHtml()%>